package api

import "example.com/m/v2/types"

func (api *App) DesignCate(req *types.DesignCateReq) (res *types.DesignCateResp) {
	res = &types.DesignCateResp{}
	res.Result = api.svcCtx.Cate.GetList(req)
	res.Success()
	return res
}

func (api *App) DesignImgs(req *types.DesignImgsReq) (res *types.DesignImgsResp) {
	res = &types.DesignImgsResp{}
	imgs, total := api.svcCtx.Imgs.GetListByPage(req)
	res.Result.List = imgs
	res.Result.Total = total
	res.Success()
	return
}

func (api *App) DesignList(req *types.DesignListReq) (res *types.DesignListResp) {
	res = &types.DesignListResp{}
	template, total := api.svcCtx.Template.GetListByPage(req)
	res.Result.List = template
	res.Result.Total = total
	res.Success()
	return
}

func (api *App) DesignMaterial(req *types.DesignMaterialReq) (res *types.DesignMaterialResp) {
	res = &types.DesignMaterialResp{}
	material, total := api.svcCtx.Material.GetListByPage(req)
	res.Result.List = material
	res.Result.Total = total
	res.Success()
	return
}

func (api *App) DesignTemp(req *types.DesignTempReq) (res *types.DesignTempResp) {
	res = &types.DesignTempResp{}
	res.Result = api.svcCtx.Template.GetById(req.Id)
	res.Success()
	return
}

func (api *App) DesignFonts(req *types.DesignFontsReq) (res *types.DesignFontsResp) {
	res = &types.DesignFontsResp{}
	fonts, total := api.svcCtx.Fonts.GetListByPage(req)
	res.Result.List = fonts
	res.Result.Total = total
	res.Success()
	return
}

func (api *App) Login(req *types.LoginReq) (res *types.LoginResp) {
	res = &types.LoginResp{}
	user := api.svcCtx.User.GetByUsername(req.Username)
	if req.Password != user.Password {
		res.Fail(-1, "username or password is incorrect")
		return
	}

	token, err := api.svcCtx.Token.Generate(user)
	if err != nil {
		res.Fail(-1, "token is invalid")
		return
	}
	res.Result.Token = token
	res.Success()
	return
}

//----------------------------------------------------------------
//need login

func (api *App) UserImage(req *types.UserImageReq, userId int64) (res *types.UserImageResp) {
	res = &types.UserImageResp{}
	image, total := api.svcCtx.Image.GetListByPage(req, userId)
	res.Result.List = image
	res.Result.Total = total
	res.Success()
	return
}

func (api *App) AddImage(req *types.AddImageReq, userId int64) (res *types.BaseResp) {
	res = &types.BaseResp{}
	api.svcCtx.Image.Create(req, userId)
	res.Success()
	return
}

func (api *App) ImageDel(req *types.ImageDelReq, userId int64) (res *types.BaseResp) {
	res = &types.BaseResp{}
	image := api.svcCtx.Image.GetById(req.Id)
	if image.Id == 0 || image.UserId != userId {
		return // id错误
	}

	api.svcCtx.Image.Delete(req)
	res.Success()
	return
}

func (api *App) DesignMy(req *types.DesignMyReq, userId int64) (res *types.DesignMyResp) {
	res = &types.DesignMyResp{}
	poster, total := api.svcCtx.Poster.GetListByPage(req, userId)
	res.Result.List = poster
	res.Result.Total = total
	res.Success()
	return
}

func (api *App) DesignPoster(req *types.DesignPosterReq, userId int64) (res *types.DesignPosterResp) {
	res = &types.DesignPosterResp{}
	poster := api.svcCtx.Poster.GetById(req.Id)
	if poster.UserId != userId {
		res.Fail(-1, "无权查看")
		return
	}
	res.Result = poster
	res.Success()
	return
}

func (api *App) DesignSave(req *types.DesignSaveReq, userId int64) (res *types.DesignSaveResp) {
	res = &types.DesignSaveResp{}
	poster := api.svcCtx.Poster.Create(req, userId)
	res.Id = poster.Id
	res.Success()
	return
}

func (api *App) PosterDel(req *types.PosterDelReq, userId int64) (res *types.BaseResp) {
	res = &types.BaseResp{}
	poster := api.svcCtx.Poster.GetById(req.Id)
	if poster.Id == 0 || poster.UserId != userId {
		res.Fail(-1, "id错误")
		return
	}

	api.svcCtx.Poster.Delete(req.Id)
	res.Success()
	return
}
